package la.bbs558.controller.base;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author: WeiHanmin
 * @description: 返回
 * @time: 2021-09-13 11:36:33
 */
@Data
@Accessors(chain = true)
public class Result<T> {

    private T data;
    private String msg;
    private Integer code;

    public Result(String msg) {
        this.msg = msg;
        code = CodeEnum.SUCCESS.getCode();
    }

    public Result(T data, String msg) {
        this.data = data;
        this.msg = msg;
        code = CodeEnum.SUCCESS.getCode();
    }

    public Result(T data, String msg, Integer code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }

    public static <T> Result<T> error(T data, CodeEnum codeEnum) {
        return new Result<>(data, codeEnum.getDesc(), codeEnum.getCode());
    }

    public static <T> Result<T> error(T data, String msg) {
        return new Result<>(data, msg, CodeEnum.SYSTEM.getCode());
    }


    public static <T> Result<T> error(String msg) {
        return new Result<>(null, msg, CodeEnum.SYSTEM.getCode());
    }

    public static <T> Result<T> error(CodeEnum codeEnum) {
        return new Result<>(null, codeEnum.getDesc(), codeEnum.getCode());
    }

    public static <T> Result<T> success(T data, String msg) {
        return new Result<>(data, msg, CodeEnum.SUCCESS.getCode());
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data,CodeEnum.SUCCESS.getDesc(), CodeEnum.SUCCESS.getCode());
    }

    public static <T> Result<T> success() {
        return new Result<>(null,CodeEnum.SUCCESS.getDesc(), CodeEnum.SUCCESS.getCode());
    }


}
